package com.bls.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpRequest {

    public final static String MsgBusBase = "http://192.168.88.47:8081/messageBus/message/publicMessage";

    /**
     * 发送get请求
     * @param url
     * @return
     * @throws Exception
     */
    public static String get(String url) throws Exception {
        URL httpUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
        StringBuilder content = new StringBuilder();
        if (connection.getResponseCode() == 200) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String str;
            while ((str = bufferedReader.readLine()) != null) {
                content.append(str);
            }
            bufferedReader.close();
            return content.toString();
        } else {
            System.err.println(connection.getResponseCode());
        }
        return "";
    }

    /**
     * 发送post请求，请求参数为urlencode格式
     * @param url
     * @param params 类似 name=xxx&age=18
     * @return
     * @throws Exception
     */
    public static String post(String url, String params) throws Exception {
        URL httpUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
        connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.connect();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
        bufferedWriter.write(params);
        bufferedWriter.flush();
        bufferedWriter.close();
        StringBuilder content = new StringBuilder();
        if (connection.getResponseCode() == 200) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String str;
            while ((str = bufferedReader.readLine()) != null) {
                content.append(str);
            }
            bufferedReader.close();
            return content.toString();
        } else {
            System.err.println(connection.getResponseCode());
        }
        return "";
    }

    /**
     * 发送post请求，请求参数为json格式
     * @param url
     * @param params 类似 {"name":"xxx","age":18}
     * @return
     * @throws Exception
     */
    public static String postJson(String url, String params) throws Exception {
        URL httpUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
        connection.setRequestProperty("content-type", "application/json;charset=utf-8");
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.connect();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
        bufferedWriter.write(params);
        bufferedWriter.flush();
        bufferedWriter.close();
        StringBuilder content = new StringBuilder();
        if (connection.getResponseCode() == 200) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String str;
            while ((str = bufferedReader.readLine()) != null) {
                content.append(str);
            }
            bufferedReader.close();
            return content.toString();
        } else {
            System.err.println(connection.getResponseCode());
        }
        return "";
    }

    // 1.application/json
    // 2.application/x-www-form-urlencoded
    // 3.multipart/form-data; boundary=xxxxxxx
    // 第一种，就是使用json的格式发送请求参数，类似{"name":"xxx","age":18}
    // 第二种，使用urlencode的形式发送请求参数，类似name=xxx&age=18
    // 第三种，支持上传文件
}
